#!/bin/sh

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

export JAVA_HOME=/usr/java/jdk1.7.0_51
#export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib:/jre/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$HOMR/bin::$ANT_HOME/bin

tomcatdir='/usr/local/tomcat'
starttomcat=$tomcatdir'/bin/startup.sh'
shutdowntomcat=$tomcatdir'/bin/shutdown.sh'
url=http://localhost:8080/login
logfile=/tmp/tomcaterror.log

tomcatid=$(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|grep -v $0|awk '{print $2}')
tomcatid_num=$(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|grep -v $0|awk '{print $2}'|wc -l)

if [ $tomcatid_num != 0 ]
then
    #执行停止命令
    $shutdowntomcat > /dev/null
    sleep 5s

    #检查进程是否存在
    while [[ $(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|grep -v $0|awk '{print $2}'|wc -l) != 0 ]]
    do
        #如果进程还存在，就直接kill
        kill -9 $tomcatid
        sleep 3s
    done
fi

$starttomcat > /dev/null
sleep 30s

num=0
while [[ $num -le 6 ]]
do
    #请求测试url,得到返回状态吗
    code=$(curl -s -o $logfile -m 10 --connect-timeout 10 $url -w %{http_code})
    if [ $code -eq 200 ]
    then
        #如果返回200
        echo "[success]页面返回码为$code,tomcat启动成功,测试页面正常......"
	break
    else
        if [ $num -eq 6 ]
        then
            echo "[error]tomcat页面出错,请注意......状态码为$code,错误日志已输出到$logfile"
        fi
    fi

    num=$[ $num + 1 ]
    sleep 5s
done
